home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xsok-1.000 / xsok-1 / xsok-1.01 / src / X-gfx.c < prev    next >
C/C++ Source or Header  |  1995-11-03  |  5KB  |  189 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    Xsok version 1.00 -- module X-gfx.c                     */
  5. /*                                         */
  6. /*    Drawing routines for the X window system.                 */
  7. /*    Written by Michael Bischoff (mbi@mo.math.nat.tu-bs.de)             */
  8. /*    November-1994                                 */
  9. /*    see COPYRIGHT.xsok for Copyright details                 */
  10. /*                                         */
  11. /*                                         */
  12. /*****************************************************************************/
  13. #include "X-sok.h"
  14.  
  15. /*#include <X11/X.h>*/
  16. #include <xpm.h>
  17.  
  18. #ifndef DELAY
  19. #define DELAY 50 /* time to sleep (in ms) between auto-moves & replay */
  20. #endif
  21.  
  22. Display *dpy;
  23. Window table = 0;
  24. struct graphic graphic;
  25. int gamegraphic = 1;
  26.  
  27. static Pixmap floor, objd, objclip;
  28. static GC gc;
  29. static int resize_pending = 0;
  30.  
  31. #ifndef HAVE_USLEEP
  32. #include <sys/time.h>
  33. /* usleep emulation code taken from xboing-2.2 by Justin C. Kibell */
  34.  
  35. static int usleep(unsigned long usec) {
  36. #ifdef SYSV
  37. #ifdef __clipper__
  38.     struct timeval tv;
  39.     tv.tv_sec=((usec)/1000);
  40.     tv.tv_usec=(((usec)%1000)*1000);
  41.     select(1,NULL,NULL,NULL,&tv);
  42. #else
  43.     poll((struct poll *) 0, (size_t) 0, usec / 1000);   /* ms resolution */
  44. #endif
  45. #else
  46.     struct timeval timeout;
  47.     timeout.tv_usec = usec % (unsigned long) 1000000;
  48.     timeout.tv_sec = usec / (unsigned long) 1000000;
  49.     select(0, (void *) 0, (void *) 0, (void *) 0, &timeout);
  50. #endif
  51.     return 0;
  52. }
  53. #endif
  54.  
  55.  
  56. void sync_and_wait(void) {
  57.     XSync(dpy, 0);
  58.     usleep(DELAY*1000);
  59. }
  60.  
  61. void NewLevel(int levelnr) {
  62.     if (levelnr < 1)
  63.     levelnr = 1;
  64.     if (levelnr > maxlevel)
  65.     levelnr = maxlevel;
  66.     game.level = levelnr;
  67.     ParseMapFile();
  68.     SetTitle();
  69.     if (table) {
  70.     init_layout();
  71.     AskWidgetForResize(graphic.width, graphic.height);
  72.     cmd_Resize();
  73.     if (gamegraphic)
  74.         refresh_screen();
  75.     resize_pending = 1;
  76.     }
  77. }
  78.  
  79. void init_layout(void) {
  80.     graphic.width = game.numcols * DX;
  81.     graphic.height = game.numrows * DY;
  82. }
  83.  
  84. void init_gfx(const char *xpmdir) {
  85.     int screen, retcode;
  86.     char s[MAXXSOKDIRLEN+14];
  87.     screen = DefaultScreen(dpy);
  88.     gc = XDefaultGC(dpy, screen);
  89.     sprintf(s, "%s/floor.xpm", xpmdir);
  90.     if ((retcode = XpmReadFileToPixmap(dpy, RootWindow(dpy, screen),
  91.                        s,  &floor, 0, NULL)) == XpmSuccess) {
  92.     sprintf(s, "%s/objects.xpm", xpmdir);
  93.     retcode = XpmReadFileToPixmap(dpy, RootWindow(dpy, screen), s, &objd,
  94.                 &objclip, NULL);
  95.     }
  96.     switch (retcode) {
  97.     case XpmSuccess:
  98.     return;        /* no error */
  99.     case XpmColorFailed:
  100.     case XpmColorError:
  101.     fatal("Not enough colors for %s", s);
  102.     case XpmOpenFailed:
  103.     fatal("Cannot open %s", s);
  104.     case XpmFileInvalid:
  105.     fatal("Invalid File: %s", s);
  106.     case XpmNoMemory:
  107.     fatal("Out of memory reading %s", s);
  108.     default:
  109.     fatal("Unknown error (code %d) reading %s", s);
  110.     }
  111. }
  112.  
  113. #define ODRAW(c) {   \
  114.     XSetClipOrigin(dpy, gc, x*DX - ((c)&3)*DX, y*DY - ((c)>>2)*DY);\
  115.     XCopyArea(dpy, objd, table, gc, DX*((c)&3), DY*((c)>>2), DX, DY, (x)*DX, (y)*DY); }
  116.  
  117. static void do_redraw(int x, int y) {
  118.     if (x >= game.numcols || y >= game.numrows || map[y][x]->pic == 16)
  119.     return; /* x = y = 0; */
  120. #ifndef SIMPLE_WALLS
  121.     if (!map[y][x]->pic) {
  122.     int b;
  123.     b = 0;
  124.     if (!map[y][x-1]->pic)
  125.         b += 1;
  126.     if (!map[y+1][x]->pic)
  127.         b += 2;
  128.     if (!map[y][x+1]->pic)
  129.         b += 4;
  130.     if (!map[y-1][x]->pic)
  131.         b += 8;
  132.     XCopyArea(dpy, floor, table, gc,
  133.           DX * (b & 7), DY * (b / 8), DX, DY, (x)*DX, (y)*DY);
  134.     } else
  135. #endif
  136.     XCopyArea(dpy, floor, table, gc, DX * (map[y][x]->pic&7),
  137.           DY * (map[y][x]->pic >> 3), DX, DY, (x)*DX, (y)*DY);
  138.     if (obj[y][x]) {
  139.     int c;
  140.     XSetClipMask(dpy, gc, objclip);
  141.     c = obj[y][x]->pic;
  142.     ODRAW(c);
  143.     XSetClipMask(dpy, gc, None);
  144.     }
  145. }
  146.  
  147. static void dotPaint(int minx, int miny, int maxx, int maxy) {
  148.     int x, y;
  149.     if (minx > maxx || miny > maxy) {
  150.     int h;
  151.     h = minx; minx = maxx; maxx = h;
  152.     h = miny; miny = maxy; maxy = h;
  153.     }
  154.     x = minx-1;
  155.     do {
  156.     ++x;
  157.     for (y = miny; y <= maxy; ++y) {
  158.         do_redraw(x, y);
  159.     }
  160.     } while (x != maxx);
  161. }
  162.  
  163. /* doPaint: just request it */
  164. void doPaint(int minx, int miny, int maxx, int maxy) {
  165. #if 0
  166.     XClearArea(dpy, table, minx*DX, miny*DY, (maxx+1)*DX-1, (maxy+1)*DY-1, True);
  167. #else
  168.     dotPaint(minx, miny, maxx, maxy);
  169. #endif
  170. }
  171.  
  172. void redraw_table(XExposeEvent *xev) {
  173.     int minx, miny, maxx, maxy;
  174.     if (resize_pending) {
  175.     if (xev->count)
  176.         return;
  177.     /* after a resize, do a complete redraw */
  178.     XClearArea(dpy, table, 0, 0, 0, 0, False);
  179.     dotPaint(1, 1, game.numcols-2, game.numrows-2);
  180.     resize_pending = 0;
  181.     } else {
  182.     minx = xev->x / DX;
  183.     miny = xev->y / DY;
  184.     maxx = (xev->x + xev->width - 1) / DX;
  185.     maxy = (xev->y + xev->height - 1) / DY;
  186.     dotPaint(minx, miny, maxx, maxy);
  187.     }
  188. }
  189.